<?php
declare(strict_types=1);
namespace App\Dao;

use App\Model\AdminLog;
use App\Model\Channel;
use App\Model\ChannelArea;
use Hyperf\Utils\Context;

class AdminLogDao
{

    /**
     * 获取管理员日志列表
     * @param array $where
     * @param array $adminIds
     * @param string[] $columns
     * @param int $pageSize
     * @param null $page
     * @return array
     */
    public function getListPage($where=[], $adminIds=[],  $page=null, $pageSize=15, $columns=['admin_log.id','admin_log.admin_id', 'admin_log.operate', 'admin_log.add_time', 'admin.name', 'admin.remark']){

        if(empty($adminIds)){//没有指定管理员id
            $data = AdminLog::query()->leftJoin('admin', 'admin_log.admin_id', '=', 'admin.id')
                ->where($where)->orderBy('admin_log.id', 'dosc')
                ->paginate(intval($pageSize), $columns, 'page', $page)
                ->toArray();
            $count = AdminLog::query()->where($where)->count();

        }else{//指定管理员id
            $data = AdminLog::query()->leftJoin('admin', 'admin_log.admin_id', '=', 'admin.id')
                ->where($where)->whereIn('admin_log.admin_id', $adminIds)->orderBy('admin_log.id', 'dosc')
                ->paginate(intval($pageSize),$columns, 'page', $page)
                ->toArray();
            $count = AdminLog::query()->where($where)->whereIn('admin_log.admin_id', $adminIds)->count();
        }

        return [
            'list' => @$data['data'],
            'count' => $count
        ];
    }


    /**
     * 新建
     * @param $insertArray
     * @return int
     */
    public function add($insertArray){
        return AdminLog::query()->insertGetId($insertArray);

    }

    /**
     * 添加管理员操作日志
     * @param $operate
     * @param int $adminId
     * @return int
     */
    public function addLog($operate, $adminId = 0){

        if(empty($adminId)){
            $user = Context::get('jwt_token');
            $adminId = $user['uid'] ? $user['uid'] : 0;
        }

        return $this->add([
            'admin_id' => $adminId,
            'operate' => $operate,
            'add_time' => time()
        ]);
    }
}